const ProjectBiz = require('../../helpers/project_biz.js');
const pageHelper = require('../../modules/research/helpers/page_helper.js');
const { createStoreBindings } = require('mobx-miniprogram-bindings');
const { searchStore } = require('../../store/search_store.js');

Page({
	data: {
		type: '', // 来自哪个业务标识
		returnUrl: '', //搜索完返回哪个地址
		cacheName: '', //本业务搜索历史缓存
		search: '', //搜索关键字
		source: '', // 来源
		hisKeys: []
	},

	onLoad: function (options) {
		let type = options.type;
		let returnUrl = options.returnUrl;
		let cacheName = 'SERACH_HIS_' + type;

		this.setData({
			type,
			cacheName,
			returnUrl
		});

		if (options && options.source) {
			this.setData({
				source: options.source,
			});
		}

		// 创建MobX绑定
		this.storeBindings = createStoreBindings(this, {
			store: searchStore,
			fields: ['searchHistory', 'hotKeywords', 'keyword'],
			actions: ['addToHistory', 'clearHistory', 'setKeyword', 'initSearchHistory', 'getHistoryByType', 'removeHistoryItem']
		});

		// 初始化搜索历史
		this.initSearchHistory();

		// 根据类型获取对应的历史记录
		const hisKeys = this.getHistoryByType(type);
		this.setData({
			hisKeys
		});
		
		// 如果有传入的关键词，直接设置
		if (options.keyword) {
			this.setData({
				search: options.keyword
			});
			this.setKeyword(options.keyword);
		}
	},

	onReady: function () {
		ProjectBiz.initPage(this);

		let curPage = pageHelper.getPrevPage(1);
		if (!curPage) return;
		if (curPage.options && curPage.options.source == 'admin') {
			wx.setNavigationBarColor({ //管理端顶部
				backgroundColor: '#2499f2',
				frontColor: '#ffffff',
			});
		}
	},

	onUnload: function () {
		// 解除MobX绑定
		this.storeBindings.destroyStoreBindings();
	},

	url: function (e) {
		pageHelper.url(e, this);
	},

	// 点击确认搜索
	bindSearchConfirm: function (e) {
		if (!this.data.type) return;

		let search = this.data.search.trim();
		if (!search) return;

		// 使用MobX添加历史记录
		const historyKey = this.data.type + '_' + search;
		this.addToHistory(historyKey);

		// 更新当前页面的历史记录显示
		const hisKeys = this.getHistoryByType(this.data.type);
		this.setData({
			search,
			hisKeys
		});

		// 设置MobX中的关键词
		this.setKeyword(search);

		// 返回上一页并传递搜索关键词
		let prevPage = pageHelper.getPrevPage();
		prevPage.setData({
			search,
		});
		wx.navigateBack();
	},

	// 清空搜索记录
	bindDelHisTap: function (e) {
		this.clearHistory();
		this.setData({
			hisKeys: []
		});
	},

	// 清除关键字
	bindClearKeyTap: function (e) {
		this.setData({
			search: ''
		});
	},

	// 点击历史
	bindKeyTap: function (e) {
		let search = e.currentTarget.dataset.key.trim();
		if (search) {
			this.setData({
				search
			});
			this.bindSearchConfirm(e);
		}
	},
	
	// 删除单条历史记录
	bindDelHistoryItem: function(e) {
		const key = e.currentTarget.dataset.key;
		if (key) {
			// 删除MobX中的记录
			const historyKey = this.data.type + '_' + key;
			this.removeHistoryItem(historyKey);
			
			// 更新显示的历史记录
			const hisKeys = this.getHistoryByType(this.data.type);
			this.setData({
				hisKeys
			});
		}
	},
	
	// 输入搜索关键词
	bindSearchInput: function(e) {
		this.setData({
			search: e.detail.value
		});
	},
	
	// 点击热门搜索词
	bindHotKeywordTap: function(e) {
		const keyword = e.currentTarget.dataset.keyword;
		if (keyword) {
			this.setData({
				search: keyword
			});
			this.bindSearchConfirm(e);
		}
	}
});